﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class AfterAnalysisController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /AfterAnalysis/

        public ActionResult Index(int uid, string pdate)
        {
            BenefitHelper.Sys.Operator op = db.Operators.Find(uid);
            ViewBag.op = op;
            ViewBag.queryuser = uid;
            int year = Convert.ToInt32(pdate.Substring(0, 4));
            ViewBag.queryyear = pdate.Substring(0, 4);
            ViewBag.pdate = pdate;
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            ViewBag.datequery = cookie.GetCookie("querydate");

            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));
            if (BenefitHelper.Sys.VisitPrower.HasOperatorPrower(operId, uid, db))
            {
                if (pdate.Length > 4)//查询月数据
                {


                    ViewBag.querymonth = pdate.Substring(4, 2);
                    BenefitHelper.Sys.TradeHistory th = new BenefitHelper.Sys.TradeHistory();
                    string lastdate = th.GetLastTradeHistoryDateString(db);
                    ViewBag.lastdate = lastdate;

                    int month = Convert.ToInt32(pdate.Substring(4, 2));
                    var _query = db.OperatorMonthSun.Where(a => a.OperatorId == uid).Where(a => a.Year == year).Where(a => a.Month == month).ToList();
                    if (_query.Count == 0)
                    {
                        return RedirectToAction("Index/" + uid + "/" + pdate, "Error", null);
                    }
                    ViewBag.sum = _query[0];
                    //    ("sssss" + _query.Count.ToString()).Debug();
                    int yyear = 0;
                    int ymonth = 0;
                    if (month == 1)
                    {
                        yyear = year - 1;
                        ymonth = 12;
                    }
                    else
                    {
                        yyear = year;
                        ymonth = month - 1;
                    }
                    var query = db.OperatorMonthSun.Where(a => a.OperatorId == uid).Where(a => a.Year == yyear).Where(a => a.Month == ymonth).ToList();
                    if (query.Count > 0)
                    {
                        ViewBag.ysum = query[0];
                    }
                    else
                    {
                        ViewBag.ysum = new BenefitHelper.View.OperatorMonthSum();
                    }
                    var queryqunian = db.OperatorMonthSun.Where(a => a.OperatorId == uid).Where(a => a.Year == (year - 1)).Where(a => a.Month == month).ToList();
                    if (queryqunian.Count > 0)
                    {
                        ViewBag.quniansum = queryqunian[0];
                    }
                    else
                    {
                        ViewBag.quniansum = new BenefitHelper.View.OperatorMonthSum();
                    }
                    var querynianjunzhi = db.OperatorYearSum.Where(a => a.OperatorId == uid).Where(a => a.Year == (year)).ToList();
                    if (querynianjunzhi.Count > 0)
                    {
                        ViewBag.querynianjunzhi = querynianjunzhi[0];
                    }
                    else
                    {
                        ViewBag.querynianjunzhi = new BenefitHelper.View.OperatorYearSum();
                    }

                    var querybatchavgmonth = db.BatchAvgMonth.Where(a => a.BatchId == op.BatchId).Where(a => a.Month == month).Where(a => a.Year == year).ToList();
                    if (querybatchavgmonth.Count > 0)
                    {
                        ViewBag.querybatchavgmonth = querybatchavgmonth[0];
                    }
                    else
                    {
                        ViewBag.querybatchavgmonth = new BenefitHelper.Data.Month.BatchAvgMonth();
                    }

                    BenefitHelper.Sys.DayGroupOperators dgos = new BenefitHelper.Sys.DayGroupOperators();
                    int groupid = dgos.GetGroupIdInMonth(uid, month, year, db);
                    var querchangegroup = db.GroupAvgMonth.Where(a => a.GroupId == groupid).Where(a => a.Year == year).Where(a => a.Month == month).ToList();
                    //("dsafaaaaaa" + groupid.ToString() + year.ToString() + month.ToString()).Debug();
                    if (querchangegroup.Count > 0)
                    {
                        ViewBag.querchangegroup = querchangegroup[0];

                    }
                    else
                    {
                        ViewBag.querchangegroup = new BenefitHelper.Data.Month.GroupAvgMonth();
                    }

                    BenefitHelper.Data.Month.CompanyAvgMonth companyavgmonth = new BenefitHelper.Data.Month.CompanyAvgMonth().GetCompanyAvgMonth(month, year, db);
                    ViewBag.companyavgmonth = companyavgmonth;

                    var querynianleiji = db.OperatorYearSum.Where(a => a.OperatorId == uid).Where(a => a.Year == year).First();
                    ViewBag.querynianleiji = querynianleiji;

                    var querynianyuqi = db.OperatorYearExpected.Where(a => a.OperatorId == uid).Where(a => a.Year == year).First();
                    ViewBag.querynianyuqi = querynianyuqi;

                    var _aquery = db.OperatorDayChange.Where(a => a.OperatorId == uid).Where(a => a.TradeHistory.YPdate == year).Where(a => a.TradeHistory.MPdate == month).OrderBy(a => a.TradeHistory.DPdate).ToList();
                    string js = BenefitHelper.Tools.ChartAtManager.GetMonthBenefitString(_aquery);
                    ViewBag.js = js;

                    var querypublish = db.OperatorPublish.Where(a => a.OperatorId == uid).Where(a => a.TradeHistory.YPdate == year).Where(a => a.TradeHistory.MPdate == month).ToList();
                    ViewBag.allpublish = querypublish.Sum(a => a.Publish);


                }
                else//查询年数据
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = "";
                    var _query = db.OperatorYearSum.Where(a => a.OperatorId == uid).Where(a => a.Year == year).ToList();
                    if (_query.Count == 0)
                    {
                        return RedirectToAction("Index/" + uid + "/" + pdate, "Error", null);
                    }
                    ViewBag.sum = _query[0];

                    var query = db.OperatorYearSum.Where(a => a.OperatorId == uid).Where(a => a.Year == year - 1).ToList();
                    if (query.Count > 0)
                    {
                        ViewBag.ysum = query[0];
                    }
                    else
                    {
                        ViewBag.ysum = new BenefitHelper.View.OperatorYearSum();
                    }
                    var queryqunian = db.OperatorYearSum.Where(a => a.OperatorId == uid).Where(a => a.Year == (year - 1)).ToList();
                    if (queryqunian.Count > 0)
                    {
                        ViewBag.quniansum = queryqunian[0];
                    }
                    else
                    {
                        ViewBag.quniansum = new BenefitHelper.View.OperatorYearSum();
                    }

                    var querynianjunzhi = db.OperatorYearSum.Where(a => a.OperatorId == uid).ToList();
                    if (querynianjunzhi.Count > 0)
                    {
                        ViewBag.querynianjunzhi = querynianjunzhi[0];
                    }
                    else
                    {
                        ViewBag.querynianjunzhi = new BenefitHelper.View.OperatorYearSum();
                    }

                    var querybatchavgmonth = db.BatchAvgYear.Where(a => a.BatchId == op.BatchId).Where(a => a.Year == year).ToList();
                    if (querybatchavgmonth.Count > 0)
                    {
                        ViewBag.querybatchavgmonth = querybatchavgmonth[0];
                    }
                    else
                    {
                        ViewBag.querybatchavgmonth = new BenefitHelper.Data.Year.BatchAvgYear();
                    }
                    var queryugroup = db.DayGroupOperators.Where(a => a.Pdate.Year == year).Where(a => a.OperatorId == uid).OrderByDescending(a => a.Id).First();

                    var querchangegroup = db.GroupAvgYear.Where(a => a.GroupId == queryugroup.GroupId).Where(a => a.Year == year).ToList();
                    //("dsafaaaaaa" + groupid.ToString() + year.ToString() + month.ToString()).Debug();
                    if (querchangegroup.Count > 0)
                    {
                        ViewBag.querchangegroup = querchangegroup[0];

                    }
                    else
                    {
                        ViewBag.querchangegroup = new BenefitHelper.Data.Month.GroupAvgMonth();
                    }

                    BenefitHelper.Data.Year.CompanyYearAvg companyavgyear = new BenefitHelper.Data.Year.CompanyYearAvg().GetCompanyAvgYear(year, db);
                    ViewBag.companyavgmonth = companyavgyear;

                    var querynianleiji = db.OperatorYearSum.Where(a => a.OperatorId == uid).Where(a => a.Year == year).First();
                    ViewBag.querynianleiji = querynianleiji;

                    var querynianyuqi = db.OperatorYearExpected.Where(a => a.OperatorId == uid).Where(a => a.Year == year).First();
                    ViewBag.querynianyuqi = querynianyuqi;

                    var _aquery = db.OperatorMonthSun.Where(a => a.OperatorId == uid).Where(a => a.Year == year).OrderBy(a => a.Id).ToList();
                    string js = BenefitHelper.Tools.ChartAtManager.GetYearBenefitString(_aquery);
                    ViewBag.js = js;

                    var querypublish = db.OperatorPublish.Where(a => a.OperatorId == uid).Where(a => a.TradeHistory.YPdate == year).ToList();
                    ViewBag.allpublish = querypublish.Sum(a => a.Publish);
                }

            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }







            return View();
        }

    }
}
